// npm install moment --save
import moment from "moment";
import axios from "axios";
import { uploadImage, uploadFileList } from "@/api/employeeManagement/index.js"

// npm i cos-js-sdk-v5 --save
// 定义上传类型
let uploadType = 'cos'; // cos:腾讯云村粗 local: 本地存储 oss：oss存储
let COS = require('cos-js-sdk-v5');
const upUrl = "https://test-1309087243.cos.ap-chengdu.myqcloud.com/" //测试
const Bucket = 'test-1309087243' //测试
const Region = 'ap-chengdu' //测试
// const Region = 'ap-shanghai-fsi'
let cos = new COS({
  SecretId: 'AKIDbERWTIxRluqfJm6MUTHFHF2f9jCTcmsp',
  SecretKey: 'oUAva5Sn6HH7nHYSbijwHRQTdmmZDgw1',
}) //测试

// 上传  两种方式 multipartUpload => 分片上传  put => 整体上传
export function cosUploadFile(file, type) {
  let typeUrl = type ? `${type}/` : 'other/'
  let fileName = `${typeUrl}${moment().format("YYYYMMDD")}/${file.name}`;
  console.log(fileName);
  try {
    return new Promise((resolve, reject) => {
      cos.putObject({
        Bucket: Bucket,
        Region: Region,
        Key: fileName,
        StorageClass: 'STANDARD',
        Body: file.raw, // 上传文件对象
        onProgress: function (progressData) { }//查看上传进度
      }, function (err, data) {//成功失败的回调
        if (data.statusCode != 200) {
          reject({ code: 500, msg: '上传失败！', err: err })
        } else {
          resolve({ code: 200, url: 'https://' + decodeURI(data.Location), msg: '上传成功！', data: data });
        }
      });
    })
  } catch (err) {
    console.log(err)
  }
}
// 删除对象
export function deleteObject(key) {
  // return 
  console.log(decodeURI(key));
  let upKey = key.split(upUrl)[1]
  return new Promise((resolve, reject) => {
    cos.deleteObject({
      Bucket: Bucket,
      Region: Region,
      Key: decodeURI(upKey),
    }, function (err, data) {
      console.log(err, data);
      if (data.statusCode != 200 && data.statusCode != 204) {
        reject({ code: 500, msg: '删除失败！', err: err })
      } else {
        resolve({ code: 200, msg: '删除成功！', data: data });
      }
    });
  })
}